import { lastValueFrom } from "rxjs";
import { Injectable } from "@nestjs/common";

import {
  RestDto,
  Voucher,
  ENUM_APP,
  VoucherGroup,
  getVoucherRpcDto,
  VoucherRpcGetPath,
  getVoucherGroupRpcDto,
  VoucherComRpcInterface,
  VoucherRpcGetGroupPath,
} from "qqlx-core";

import { BusRpc } from "./app.droplet-bus";
import { RpcWaiting } from "./rpc.response";
import { RpcConnection } from "./com.connection";

@Injectable()
//
export class RiverVoucherRpc extends RpcConnection implements VoucherComRpcInterface<RestDto> {
  constructor(readonly BusRpc: BusRpc) {
    super(BusRpc, ENUM_APP.RIVER_VOUCHER);
  }

  @RpcWaiting(VoucherRpcGetGroupPath)
  async getGroup(dto: RestDto & getVoucherGroupRpcDto) {
    const ob = this.CLIENT.send(VoucherRpcGetGroupPath, dto);
    return lastValueFrom<{ entityLs: VoucherGroup[] }>(ob);
  }

  @RpcWaiting(VoucherRpcGetPath)
  async get(dto: RestDto & getVoucherRpcDto) {
    const ob = this.CLIENT.send(VoucherRpcGetPath, dto);
    return lastValueFrom<{ entityLs: Voucher[] }>(ob);
  }
}
